<template>
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" style="margin-top: 5px"></i>
      <span style="margin-top: 5px">菜单列表</span>
      <el-button class="btn-add" @click="handleAddMenu()" size="mini"
        >添加</el-button
      >
    </el-card>
    <div class="table-container">
      <el-table
        ref="menuTable"
        size="mini"
        style="width: 100%"
        :data="list"
        v-loading="listLoading"
        :height="tableHeight"
        border
        row-key="id"
        :default-expand-all="true"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column
          prop="title"
          label="菜单名称"
          width="260"
          align="left"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column label="菜单类型" min-width="100" align="center">
          <template slot-scope="scope">{{
            scope.row.type | formatMenuType
          }}</template>
        </el-table-column>
        <!-- <el-table-column label="菜单级数" min-width="100" align="center">
          <template slot-scope="scope">{{ scope.row.level | levelFilter }}</template>
        </el-table-column> -->
        <el-table-column label="前端图标" min-width="100" align="center">
          <template slot-scope="scope"
            ><svg-icon :icon-class="scope.row.icon"></svg-icon
          ></template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="路由地址"
          min-width="160"
          align="left"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column label="排序" min-width="100" align="center">
          <template slot-scope="scope">{{ scope.row.sort }}</template>
        </el-table-column>
        <el-table-column label="是否显示" min-width="100" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleHiddenChange(scope.$index, scope.row)"
              :active-value="0"
              :inactive-value="1"
              v-model="scope.row.hidden"
            >
            </el-switch>
          </template>
        </el-table-column>
        <!-- <el-table-column label="操作" width="160" align="center" >
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleAddMenu(scope.row)">添加</el-button>
            <el-button size="mini" type="text" @click="handleUpdate(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column> -->
      </el-table>
    </div>
    <edit-obj-dialog
      ref="editObjDialog"
      v-if="dialogVisible"
      :visible.sync="dialogVisible"
      :isEdit="isEdit"
      :dataId="dataId"
      @handle-confirm="handleConfirm"
    >
    </edit-obj-dialog>
  </div>
</template>

<script>
import { listAll, deleteMenu, updateHidden } from "@/api/menu";
import { handleTree } from "@/utils/tree";
import EditObjDialog from "./dialogs/edit-obj-dialog.vue";

export default {
  name: "menuList",
  components: {
    EditObjDialog
  },
  data() {
    return {
      tableHeight: 750,
      list: null,
      listLoading: true,
      dialogVisible: false,
      dataId: null,
      isEdit: false
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 140;
    });
    this.getList();
  },
  filters: {
    levelFilter(value) {
      if (value === 0) {
        return "一级";
      } else if (value === 1) {
        return "二级";
      } else if (value === 2) {
        return "三级";
      }
    },
    formatMenuType(type) {
      if (type === 0) {
        return "菜单";
      } else {
        return "按钮";
      }
    }
  },
  methods: {
    handleAddMenu(row) {
      this.isEdit = false;
      if (row) {
        this.dataId = row.id;
      }
      this.dialogVisible = true;
    },
    handleConfirm() {
      this.getList();
      this.dialogVisible = false;
    },
    getList() {
      this.listLoading = true;
      listAll().then(response => {
        this.listLoading = false;
        this.list = handleTree(response.data, "id");
      });
    },
    handleHiddenChange(index, row) {
      updateHidden(row.id, { hidden: row.hidden }).then(response => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000
        });
      });
    },
    handleUpdate(index, row) {
      this.dialogVisible = true;
      this.isEdit = true;
      this.dataId = row.id;
    },
    handleDelete(index, row) {
      this.$confirm("是否要删除该菜单", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteMenu(row.id).then(response => {
          this.$message({
            message: "删除成功",
            type: "success",
            duration: 1000
          });
          this.getList();
        });
      });
    }
  }
};
</script>

<style scoped></style>
